home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / dashed-capped lines / dashed-capped lines.p < prev    next >
Encoding:
Text File  |  2000-09-28  |  6.6 KB  |  244 lines

  1. {
  2.     File:        Dashed-capped Lines.p
  3.  
  4.     Contains:    Dashed-capped Lines demonstrates how to draw dashed line objects on PostScript
  5.                 printers.  It also demonstrates how to use PostScript line caps with dashed
  6.                 lines.
  7.                 This simple example does not deal with QuickDraw printers, on which it draws
  8.                 solid black lines.
  9.         
  10.                  (This is my Dashed Lines sample modified to use setlinecap to make round-ended lines.)
  11.  
  12.  
  13.     Written by: Dave Hersey    
  14.  
  15.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  16.  
  17.                 You may incorporate this Apple sample source code into your program(s) without
  18.                 restriction. This Apple sample source code has been provided "AS IS" and the
  19.                 responsibility for its operation is yours. You are not permitted to redistribute
  20.                 this Apple sample source code as "Apple sample source code" after having made
  21.                 changes. If you're going to re-distribute the source, we require that you make
  22.                 it clear in the source that the code was descended from Apple sample source
  23.                 code, but that you've made changes.
  24.  
  25.     Change History (most recent first):
  26.                 7/23/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  27.                 
  28.  
  29. }
  30.  
  31. PROGRAM Dashed_Lines;
  32.  
  33. USES
  34.     Fonts, QuickDraw, Printing;
  35.  
  36.  
  37. CONST
  38.     DashedLine            = 180;    (*    Begin PostScript line dashing            *)
  39.     DashedStop            = 181;    (*    End PostScript line dashing                *)
  40.     SetLineWidth        = 182;    (*    Set high resolution line width.         *)    
  41.     PostScriptHandle    = 192;    (*    Transfer a block of PostScript            *)    
  42.  
  43. TYPE
  44.     TDashedLineHdl = ^TDashedLinePtr;
  45.     TDashedLinePtr = ^TDashedLine;
  46.     TDashedLine = PACKED RECORD
  47.         offset:        SignedByte;
  48.         centered:    SignedByte;
  49.         dashed:        ARRAY [0..1] OF SignedByte;
  50.     END;
  51.  
  52.  
  53. {*------ SendPostScript ------------------------------------------------------------*}
  54.  
  55. PROCEDURE SendPostScript(theComment: Str255);
  56. VAR
  57.     PSCommand    : Str255;
  58.     CommandHdl    : Handle;
  59.     CRString    : Str255;
  60.     theError    : OSErr;
  61. BEGIN
  62.     CRString := ' ';
  63.     CRString[1] := CHR(13);
  64.     PSCommand := theComment;
  65.     PSCommand := CONCAT(PSCommand, CRString);
  66.     theError := PtrToHand(POINTER(ORD(@PSCommand) + 1), CommandHdl, LENGTH(PSCommand));
  67.     PicComment(PostScriptHandle, LENGTH(PSCommand), CommandHdl);
  68.     DisposeHandle(CommandHdl);
  69. END;
  70.  
  71.  
  72.   {*------ DrawStuff -----------------------------------------------------------------*}
  73.  
  74. {**
  75.  **      DrawStuff draws QuickDraw objects with dashed lines in Postscript.
  76.  **}
  77.  
  78.  PROCEDURE DrawStuff (theWorld : Rect; theGPort : GrafPtr);
  79.  
  80. TYPE
  81.     widhdl = ^widptr;
  82.     widptr = ^widpt;
  83.     widpt = Point;
  84.  
  85.  VAR
  86.    oldPort      :     GrafPtr;
  87.    arect        :    Rect;
  88.    Width        :    Widhdl;
  89.    dashedln     :    TDashedLineHdl;
  90.    myPic        :    PicHandle;
  91.  
  92.  
  93. BEGIN
  94.     GetPort (oldPort);
  95.  
  96.     SetPort (theGPort);
  97.     Dashedln := TDashedLineHdl(NewHandle(sizeof(tdashedline)));
  98.     Dashedln^^.offset := 0;       {No offset}
  99.     Dashedln^^.centered := 0;     {don’t center}
  100.     Dashedln^^.dashed[0] := 1;    {this is the length }
  101.     Dashedln^^.dashed[1] := 4;    {this means 4 points on, 4 points off }
  102.     
  103.     Width := widhdl(NewHandle(sizeof(widpt)));
  104.     Width^^.h := 1;                {denominator is 4}
  105.     Width^^.v := 2;               {numerator is 1}
  106.     
  107.     myPic := OpenPicture(theWorld);
  108.     PenSize(1,1);                {Set the pen size to 1 wide x 1 high }
  109.     ClipRect(theWorld);
  110.  
  111.     SendPostScript('1 setlinecap');
  112.     PicComment(DashedLine,GetHandleSize(Handle(dashedln)),Handle(dashedln)); 
  113.     PicComment(SetLineWidth,4,Handle(width));   {SetLineWidth to 8 pixels wide.}
  114.  
  115.     SetRect(arect,100,100,500,500);    {Draw some stuff in dash mode.}
  116.     FrameRect(aRect);
  117.     MoveTo(500,500);
  118.     Lineto(100,100);
  119.     MoveTo(100,500);
  120.     Lineto(500,100);
  121.  
  122.     InsetRect(arect,10,10);
  123.     FrameOval(aRect);
  124.     InsetRect(arect,10,10);
  125.     FrameOval(aRect);
  126.     InsetRect(arect,10,10);
  127.     FrameOval(aRect);
  128.     InsetRect(arect,10,10);
  129.     FrameOval(aRect);
  130.     InsetRect(arect,10,10);
  131.     FrameOval(aRect);
  132.     InsetRect(arect,10,10);
  133.     FrameOval(aRect);
  134.     InsetRect(arect,10,10);
  135.     FrameOval(aRect);
  136.     InsetRect(arect,10,10);
  137.     FrameOval(aRect);
  138.     InsetRect(arect,10,10);
  139.     FrameOval(aRect);
  140.  
  141.     PicComment(DashedStop,0,nil);    {DashedStop}
  142.  
  143.   ClosePicture;
  144.   DisposeHandle(handle(width));           {Clean up}
  145.   DisposeHandle(handle(dashedln));
  146.   DrawPicture(MyPic, theWorld);          {print it}
  147.   KillPicture(MyPic);    SetPort(oldPort);
  148.  
  149.  END;  {**  DrawStuff  **}
  150.  
  151.  
  152. {*------ PrintStuff ----------------------------------------------------------------*}
  153. {**
  154.  **        PrintStuff will call all of the nescessary Print Manager calls to print 
  155.  **        a document. It checks PrError() after each Print Manager call. If an error 
  156.  **     is found, all of the Print Manager open calls (i.e. PrOpen, PrOpenDoc...) 
  157.  **        will have a corresponding close call before the error is posted to the user. 
  158.  **        You want to use this approach to make sure the Print Manager closes properly 
  159.  **        and all temporary memory is released.
  160.  **}
  161.  
  162. PROCEDURE PrintStuff;
  163.  
  164. VAR
  165.   oldPort              : GrafPtr;
  166.   thePrRecHdl        : THPrint;
  167.   thePrPort            : TPPrPort;
  168.   theStatus            : TPrStatus;
  169.     
  170. BEGIN
  171.    GetPort(oldPort);
  172.     
  173.    thePrRecHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  174.     
  175.    IF (MemError = noErr) AND (thePrRecHdl <> NIL) THEN
  176.     BEGIN
  177.        PrOpen;
  178.        IF (PrError = noErr) THEN
  179.         BEGIN
  180.            PrintDefault(thePrRecHdl);
  181.  
  182.            IF (PrError = noErr) THEN
  183.             BEGIN
  184.                IF (PrStlDialog(thePrRecHdl)) THEN
  185.                 BEGIN
  186.                    IF (PrJobDialog(thePrRecHdl)) THEN 
  187.                     BEGIN
  188.                           thePrPort := PrOpenDoc(thePrRecHdl, NIL, NIL);
  189.                                
  190.                       IF (PrError = noErr) THEN
  191.                         BEGIN
  192.  
  193.                              PrOpenPage(thePrPort, NIL);
  194.                                 
  195.                           IF (PrError = noErr) THEN
  196.                             BEGIN
  197.                               {**
  198.                                   rPage (IM II-150) is the printable area for the  
  199.                                   currently selected printer. By passing the current  
  200.                                   port to the draw routine, enables your app
  201.                                   to use the same routine to draw to the screen
  202.                                   and the printer's GrafPort.
  203.                                **}
  204.                                     
  205.                                DrawStuff (thePrRecHdl^^.prInfo.rPage, 
  206.                                            GrafPtr (thePrPort));
  207.                                  
  208.                              END;
  209.                             PrClosePage(thePrPort);
  210.                           END;
  211.                              
  212.                           PrCloseDoc(thePrPort);
  213.                              
  214.                           IF (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  215.                                PrPicFile(thePrRecHdl, NIL, NIL, NIL, @theStatus);
  216.  
  217.                       END;
  218.                   END;
  219.               END;
  220.           END;
  221.         
  222.         PrClose;
  223.  
  224.      END;
  225.  
  226. END;  {**  PrintStuff  **}
  227.  
  228.  
  229. {*------ main ----------------------------------------------------------------------*}
  230.  
  231. BEGIN
  232.     
  233.     InitGraf(@qd.thePort);
  234.     InitFonts;
  235.     FlushEvents(everyEvent, 0);    
  236.     InitWindows;
  237.     InitMenus;
  238.     TEInit;
  239.     InitDialogs(NIL);
  240.     InitCursor;
  241.  
  242.     PrintStuff;
  243.  
  244. END. {**  main  **}